﻿using Microsoft.ExtendedReflection.ComponentModel;
using Microsoft.ExtendedReflection.Logging;
using Microsoft.ExtendedReflection.Metadata;
using Microsoft.ExtendedReflection.Utilities;
using MolesVS2012.Host;
using System;

namespace MolesVS2012.Host.Host
{
    [Serializable]
    public sealed class MolesHostEngine : Microsoft.ExtendedReflection.ComponentModel.Engine
    {
        public MolesHostEngine()
            : this((IContainer)new Container(new TypeEx[0]))
        {
        }

        public MolesHostEngine(IContainer container)
            : base(container, (IComponent)MolesVsHostOptions.FromEnvironment(), (IComponent)new EventLog())
        {
        }

        public void AddAssemblyResolver(AssemblyResolver resolver)
        {
            this.AddComponent((string)null, (IComponent)resolver);
            IEngineOptions service = this.GetService<IEngineOptions>();
            resolver.AddSearchDirectoryRange(service.GetAssemblyResolutionDirectories());
        }

        public int Execute()
        {
            try
            {
                return this.GetService<MolesHostDriver>().Execute();
            }
            catch (Exception ex)
            {
                Console.WriteLine(((object)ex).ToString());
                return -1001;
            }
        }

        protected override void AddComponents()
        {
            this.AddComponent((string)null, (IComponent)new MolesHostDriver());
            this.AddComponent((string)null, (IComponent)new MolesHostAdapter());
            this.AddComponent((string)null, (IComponent)new MolesHostRemotingManager());
            this.AddComponent((string)null, (IComponent)new RunResultSyncLogger(RemotingEntityKind.Host));
            if (!MolesVsHostEnvironmentSettings.CreateWindow.Value)
                return;
            this.AddComponent((string)null, (IComponent)new MolesHostConsoleLogger());
        }
    }
}

